package cn.virens.common.boot.dubbo.consumer.beetl.tag;

import org.beetl.core.tag.GeneralVarTagBinding;
import org.beetl.core.tag.TagFactory;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通用Beetl的Tag 创建器
 *
 * @author virens
 */
public abstract class AbstractTag<T extends AbstractTag<T>> extends GeneralVarTagBinding implements TagFactory {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    @SuppressWarnings("unchecked")
    public T createTag() {
        return (T) this;
    }

    @Override
    public void render() {
        try {
            this.render0();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    protected void render0() throws Exception {
    }

    protected String getAttributeStr(String attrName) {
        return ConvertUtil.toStr(getAttributeValue(attrName));
    }

    protected String getAttributeStr(String attrName, String def) {
        return ConvertUtil.toStr(getAttributeValue(attrName), def);
    }

    protected Integer getAttributeInt(String attrName) {
        return ConvertUtil.toInt(getAttributeValue(attrName));
    }

    protected Integer getAttributeInt(String attrName, Integer def) {
        return ConvertUtil.toInt(getAttributeValue(attrName), def);
    }

}
